package com.dhcc.bpm.alarm.utils;

import com.alibaba.druid.filter.config.ConfigFilter;
import com.alibaba.druid.pool.DruidDataSource;
import com.dhcc.bpm.alarm.entity.AlarmDbInfoConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.*;
import java.util.Arrays;
import java.util.Properties;

@Slf4j
public class SQLutil {
    public static JdbcTemplate getJdbcTemplate(AlarmDbInfoConfig alarmDbInfoConfig) {
        DruidDataSource instance = new DruidDataSource();
        instance.setBreakAfterAcquireFailure(true);        //  break-after-acquire-failure = true。设置之后数据库连接不成功也不会不断的重试。
        instance.setConnectionErrorRetryAttempts(0);       //  失败后重连次数
        try {
            instance.setUsername(alarmDbInfoConfig.getDbUser());
            instance.setPassword(alarmDbInfoConfig.getDbPswd());
            if ("01".equals(alarmDbInfoConfig.getDbtype())) {
                instance.setDriverClassName("oracle.jdbc.OracleDriver");
                instance.setUrl("jdbc:oracle:thin:@" + alarmDbInfoConfig.getDbIp() + ":" + alarmDbInfoConfig.getDbPort() + ":" + alarmDbInfoConfig.getDbInstance());
            } else if ("02".equals(alarmDbInfoConfig.getDbtype())) {
                instance.setDriverClassName("org.mariadb.jdbc.Driver");
                instance.setUrl("jdbc:mariadb://" + alarmDbInfoConfig.getDbIp() + ":" + alarmDbInfoConfig.getDbPort() + "/" + alarmDbInfoConfig.getDbInstance() + "?useUnicode=true&characterEncoding=utf-8&useSSL=false");
            } else if ("05".equals(alarmDbInfoConfig.getDbtype())) {
            }
//            instance.setDriverClassName(alarmDbInfoConfig.getDriver());
//            instance.setUrl(alarmDbInfoConfig.getUrl());
            Properties properties = new Properties();
//            if(!StringUtils.isBlank(alarmDbInfoConfig.getDbDecrypt())){
//                properties.setProperty("config.decrypt","true");
//                properties.setProperty("config.enabled","true");
//                properties.setProperty("config.decrypt.key",alarmDbInfoConfig.getDbDecrypt());
//            }
            instance.setConnectProperties(properties);
            ConfigFilter configFilter = new ConfigFilter();
            instance.setProxyFilters(Arrays.asList(configFilter));
            JdbcTemplate jdbcTemplate = new JdbcTemplate(instance);
            return jdbcTemplate;
        } catch (Exception e) {
            log.info("数据库异常......");
            log.error("数据库异常：", e);
        }
        return null;
    }

    public static void close(JdbcTemplate jdbcTemplate) {

        DruidDataSource dataSource = (DruidDataSource) jdbcTemplate.getDataSource();
        dataSource.close();
    }

    public static Connection getConnection(AlarmDbInfoConfig dbConfig) {
        try {

            String dbDriver = null;
            String dbUrl = "";
            if ("01".equals(dbConfig.getDbtype())) {
                dbDriver = "oracle.jdbc.OracleDriver";
                dbUrl = "jdbc:oracle:thin:@" + dbConfig.getDbIp() + ":" + dbConfig.getDbPort() + ":" + dbConfig.getDbInstance();
            } else if ("02".equals(dbConfig.getDbtype())) {
                dbDriver = "org.mariadb.jdbc.Driver";
                dbUrl = "jdbc:mariadb://" + dbConfig.getDbIp() + ":" + dbConfig.getDbPort() + "/" + dbConfig.getDbInstance() + "?useUnicode=true&characterEncoding=utf-8&useSSL=false";
            } else if ("05".equals(dbConfig.getDbtype())) {
            } else {
                log.info("不支持的数据库类型！");
                return null;
            }
            //1.加载驱动程序
            Class.forName(dbDriver);
            //2. 获得数据库连接
            DriverManager.setLoginTimeout(3);
            log.info("连接数据库：{}", dbUrl);
            return DriverManager.getConnection(dbUrl, dbConfig.getDbUser(), dbConfig.getDbPswd());
        } catch (ClassNotFoundException e) {
            log.error("数据库驱动错误:{}", e);
        } catch (SQLException e) {
            log.error("数据库连接异常:{}", e);
        }
        return null;
    }


    public static void release(Connection conn, Statement stmt, ResultSet result) {
        try {
            if (null != result) {
                result.close();
            }

            if (null != stmt) {
                stmt.close();
            }

            if (null != conn) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void release(Connection conn) {
        release(conn, null, null);
    }


}
